//
//  KPHUDViewController.swift
//  KPBaseProject
//
//  Created by Kipp Dev on 2018/4/7.
//  Copyright © 2018年 Kipp. All rights reserved.
//

import UIKit

class KPHUDViewController: KPBaseTableViewController {

    fileprivate let titles = ["Loading","Progress","Success","Error","Toast"]
    fileprivate var scrollView: UIScrollView!
    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return titles.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") ?? UITableViewCell.init(style: .default, reuseIdentifier: "cell")
        cell.textLabel?.text = titles[indexPath.row]
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch indexPath.row {
        case 0:
            showLoadingHUD()
            delay(2.5) {
                hideHUD()
            }
        case 1:
            testProgress(hud: showProgressHUD(), progress: 0)

        case 2:
            showSuccessHUD(message: "操作成功！", detail: "全部操作成功！")
        case 3:
            showErrorHUD(message: "操作失败！", detail: "全部操作失败！")
        case 4:
            makeToast(message: "吐司提示！")

        default:
            break
        }
    }


    fileprivate func testProgress(hud:JGProgressHUD,progress: Int) {
        let progress = progress + 1
        hud.progress = Float(progress)/100.0
        hud.detailTextLabel.text = "\(progress)% Complete"

        if progress == 100 {
            DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(500)) {
                UIView.animate(withDuration: 0.1, animations: {
                    hud.textLabel.text = "Success"
                    hud.detailTextLabel.text = nil
                    hud.indicatorView = JGProgressHUDSuccessIndicatorView()
                })

                hud.dismiss(afterDelay: 1.0)
            }
        }
        else {
            DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(20)) {
                self.testProgress(hud: hud, progress: progress)
            }
        }
    }
}
